﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        #result {
            width: 200px;
            height: 100px;
            border: solid 1px #90b;
        }
    </style>
</head>
<body>
    <div id="result"></div>

    <script>

        const result = document.getElementById('result');
        //绑定鼠标按下事件
        window.onkeydown = function () {
            //发送请求
            const xhr = new XMLHttpRequest();
            //初始化
            xhr.open('GET', 'http://127.0.0.1:8000/json-server');

            //设置响应格式
            xhr.responseType = 'json';
            //发送
            xhr.send();
            //是将绑定
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {

                    if (xhr.status >= 200 && xhr.status < 300) {

                        //console.log(xhr.response);
                        //手动对数据转化
                        //let data = JSON.parse(xhr.response);
                        //console.log(data);
                        //console.log(xhr.response);
                        result.innerHTML = xhr.response.name;
                    }
                }
            }

        }
    </script>
</body>
</html>